﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Data.SqlTypes;

namespace DataAccess.Operate
{
    public class DA_Project_SalseAgency_Operate:SqlInteractionBase
    {
        private DataEntity.T_Project_SalseAgency _objMessage = null;

        public override bool Insert(object obj)
        {
            SqlCommand cmdToExecute = new SqlCommand();
            cmdToExecute.CommandText = "dbo.[sp_t_Project_SalseAgency_Insert]";
            cmdToExecute.CommandType = CommandType.StoredProcedure;

            // Use base class' connection object
            cmdToExecute.Connection = _mainConnection;

            this._objMessage = (DataEntity.T_Project_SalseAgency)obj;
            try
            {
                cmdToExecute.Parameters.Add(new SqlParameter("@guidProject_SalseAgency_ID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Project_SalseAgency_ID));
                cmdToExecute.Parameters.Add(new SqlParameter("@guidProject_SalseAgency_MainID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Project_SalseAgency_MainID));
                cmdToExecute.Parameters.Add(new SqlParameter("@iProject_SalseAgency_TypeID", SqlDbType.Int, 4, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Project_SalseAgency_TypeID));
                cmdToExecute.Parameters.Add(new SqlParameter("@sProject_SalseAgency_FirstA1", SqlDbType.NVarChar, 20, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Project_SalseAgency_FirstA1));
                cmdToExecute.Parameters.Add(new SqlParameter("@sProject_SalseAgency_FirstA2", SqlDbType.NVarChar, 20, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Project_SalseAgency_FirstA2));
                cmdToExecute.Parameters.Add(new SqlParameter("@sProject_SalseAgency_FirstA3", SqlDbType.NVarChar, 20, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Project_SalseAgency_FirstA3));
                cmdToExecute.Parameters.Add(new SqlParameter("@sProject_SalseAgency_FirstB1", SqlDbType.NVarChar, 20, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Project_SalseAgency_FirstB1));
                cmdToExecute.Parameters.Add(new SqlParameter("@sProject_SalseAgency_FirstB2", SqlDbType.NVarChar, 20, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Project_SalseAgency_FirstB2));

                if (_mainConnectionIsCreatedLocal)
                {
                    // Open connection.
                    _mainConnection.Open();
                }
                else
                {
                    if (_mainConnectionProvider.IsTransactionPending)
                    {
                        cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
                    }
                }

                // Execute query.
                _rowsAffected = cmdToExecute.ExecuteNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                // some error occured. Bubble it to caller and encapsulate Exception object
                throw new Exception("T_Project_SalseAgency::Insert::Error occured.", ex);
            }
            finally
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    // Close connection.
                    _mainConnection.Close();
                }
                cmdToExecute.Dispose();
            }
        }

        public override object SelectOne(object obj)
        {
            SqlCommand cmdToExecute = new SqlCommand();
            cmdToExecute.CommandText = "dbo.[sp_t_Project_SalseAgency_SelectOne]";
            cmdToExecute.CommandType = CommandType.StoredProcedure;
            DataTable toReturn = new DataTable("t_Project_SalseAgency");
            SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

            // Use base class' connection object
            cmdToExecute.Connection = _mainConnection;
            this._objMessage =new DataEntity.T_Project_SalseAgency();
            try
            {
                cmdToExecute.Parameters.Add(new SqlParameter("@guidProject_SalseAgency_ID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, SqlGuid.Parse(obj.ToString())));

                if (_mainConnectionIsCreatedLocal)
                {
                    // Open connection.
                    _mainConnection.Open();
                }
                else
                {
                    if (_mainConnectionProvider.IsTransactionPending)
                    {
                        cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
                    }
                }

                // Execute query.
                adapter.Fill(toReturn);
                if (toReturn.Rows.Count > 0)
                {
                    this._objMessage.Project_SalseAgency_ID = (Guid)toReturn.Rows[0]["Project_SalseAgency_ID"];
                    this._objMessage.Project_SalseAgency_MainID = (Guid)toReturn.Rows[0]["Project_SalseAgency_MainID"];
                    this._objMessage.Project_SalseAgency_TypeID = (int)toReturn.Rows[0]["Project_SalseAgency_TypeID"];
                    this._objMessage.Project_SalseAgency_FirstA1 = toReturn.Rows[0]["Project_SalseAgency_FirstA1"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["Project_SalseAgency_FirstA1"];
                    this._objMessage.Project_SalseAgency_FirstA2 = toReturn.Rows[0]["Project_SalseAgency_FirstA2"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["Project_SalseAgency_FirstA2"];
                    this._objMessage.Project_SalseAgency_FirstA3 = toReturn.Rows[0]["Project_SalseAgency_FirstA3"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["Project_SalseAgency_FirstA3"];
                    this._objMessage.Project_SalseAgency_FirstB1 = toReturn.Rows[0]["Project_SalseAgency_FirstB1"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["Project_SalseAgency_FirstB1"];
                    this._objMessage.Project_SalseAgency_FirstB2 = toReturn.Rows[0]["Project_SalseAgency_FirstB2"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["Project_SalseAgency_FirstB2"];
                }
                return this._objMessage;
            }
            catch (Exception ex)
            {
                // some error occured. Bubble it to caller and encapsulate Exception object
                throw new Exception("T_Project_SalseAgency::SelectOne::Error occured.", ex);
            }
            finally
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    // Close connection.
                    _mainConnection.Close();
                }
                cmdToExecute.Dispose();
                adapter.Dispose();
            }
        }
    }
}
